package com.clayecloud.module_identitycard.adapter;


import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.PorterDuff;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;

import com.clayecloud.module_identitycard.R;
import com.clayecloud.module_identitycard.databinding.FragmentIdcardItemBinding;
import com.clayecloud.module_identitycard.entity.IDCard;
import com.loopeer.cardstack.CardStackView;
import com.loopeer.cardstack.StackAdapter;

import java.util.List;

public class CardStackAdapter extends StackAdapter<IDCard> {

    public CardStackAdapter(Context context, List data) {
        super(context);
        updateData(data);
    }

    @Override
    protected CardStackView.ViewHolder onCreateView(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_idcard_item,parent,false);
        CardViewHolder holder = new CardViewHolder(view, parent);
        return holder;
    }

    @Override
    public void bindView(IDCard data, int position, CardStackView.ViewHolder holder) {
        if(holder instanceof CardViewHolder)
        {
            CardViewHolder cardHolder = (CardViewHolder)holder;
            cardHolder.onBind(data, position);
        }
    }

    @Override
    public int getItemCount() {
        return super.getItemCount();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public IDCard getItem(int position) {
        return super.getItem(position);
    }

    private static class CardViewHolder extends CardStackView.ViewHolder
    {
        private View root;
        private FrameLayout cardTitle;
        private FrameLayout content;
        private ViewGroup parent;

        public CardViewHolder(View view, ViewGroup parent)
        {
            super(view);
            root = view;
            cardTitle = (FrameLayout)view.findViewById(R.id.card_title);
            content = (FrameLayout)view.findViewById(R.id.content);
            this.parent = parent;
        }

        public void onBind(IDCard idCard,int position)
        {
            FragmentIdcardItemBinding fragmentIdcardItemBinding =  DataBindingUtil.bind(root);
            fragmentIdcardItemBinding.setItem(idCard);
            // 卡片的颜色
            cardTitle.getBackground().setColorFilter(ContextCompat.getColor(getContext(),R.color.common_confirm_text_color), PorterDuff.Mode.SRC_IN);
        }

        @Override
        public void onItemExpand(boolean bool) {
            content.setVisibility(bool ? View.VISIBLE : View.GONE);
            if(bool){
                content.setAlpha(0);
                playAnimation(0,1,250);
            }
        }

        private void playAnimation(float startTime, float endTime, int playTime){
            ValueAnimator animator = ValueAnimator.ofFloat(startTime, endTime).setDuration(playTime);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    content.setAlpha((Float) animation.getAnimatedValue());
                }
            });
            animator.start();
        }

    }
}
